package com.eastshop.adminuser.action;

import org.apache.struts2.ServletActionContext;

import com.eastshop.adminuser.service.AdminUserService;
import com.eastshop.adminuser.vo.AdminUser;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
 * 后台用户（管理员）登录的Action类
 * @author admin
 *
 */
public class AdminUserAction extends ActionSupport implements ModelDriven<AdminUser>{
	// 模型驱动使用的对象
	private AdminUser adminUser = new AdminUser();

	public AdminUser getModel() {
		return adminUser;
	}
	
	// 注入后台用户的Service
	private AdminUserService adminUserService;

	public void setAdminUserService(AdminUserService adminUserService) {
		this.adminUserService = adminUserService;
	}
	
	// 后台用户登录方法
	public String login(){
		// 登录方法
		AdminUser admin = adminUserService.login(adminUser);
		// 判断
		if(admin == null){
			// 用户名或密码错误
			this.addActionError("用户名或密码错误！");
			return "loginFail";
		}else {
			// 登录成功
			ServletActionContext.getRequest().getSession().setAttribute("admin", admin);
			return "loginSuccess";
		}
	}
	
	// 后台登出的方法
	public String logout(){
		ServletActionContext.getRequest().getSession().invalidate();
		return "logout";
	}
}
